﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Public;
using BUS;

namespace QuanLyKhoHangVatTu
{
    public partial class ChiTietKho : DevExpress.XtraEditors.XtraForm, IChildForm
    {
        Kho_BUS k_bus = new Kho_BUS();
        Kho_PB k_pb = new Kho_PB();
        public ChiTietKho()
        {
            InitializeComponent();
        }

        private void ChiTietKho_Load(object sender, EventArgs e)
        {
            //Flag_PB.Flag = 2;
            //gridKho.DataSource = k_bus.LayDSKho();
            Nap();
            
        }
        public void Nap()
        {
            gridKho.DataSource = k_bus.LayDSKho();
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            //Kho_PB.MaKho = gridView1.GetRowCellValue(e.FocusedRowHandle, "MAKHO").ToString();
            //Kho_PB.TenKho = gridView1.GetRowCellValue(e.FocusedRowHandle, "TENKHO").ToString();
            //Kho_PB.DiaChi = gridView1.GetRowCellValue(e.FocusedRowHandle, "DIACHI").ToString();
        }

        public void Them()
        {
            Kho k = new Kho();
            k.Tag = this;
            k.ShowDialog();
            Nap();
        }


        public void Sua()
        {
            DataRow r = gridView1.GetFocusedDataRow();
            if (r == null) return;

            Kho k = new Kho(r["MAKHO"].ToString(), r["TENKHO"].ToString(), r["DIACHI"].ToString());

            k.Tag = this;
            k.ShowDialog();
            Nap();
        }

        public void Xoa()
        {
            DataRow r = gridView1.GetFocusedDataRow();
            if (r == null) return;
            if (XtraMessageBox.Show(string.Format("Bạn có chắc muốn xóa Kho <{0}>?"
                , r["MAKHO"]), "Xác nhận"
                 , MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                return;
            try
            {
                k_bus.XoaKho(r["MAKHO"].ToString());
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("conflicted with the REFERENCE constraint"))
                {
                    XtraMessageBox.Show("Không thể xóa vì có dữ liệu quan hệ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            Nap();
        }
    }
}